import { Type, applyDecorators } from '@nestjs/common';
import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';
import {
  CommonDataBooleanDto,
  CommonDataDto,
  CommonDataNumberDto,
  CommonDataStringDto,
} from 'src/dtos/common-data.dto';

export const ApiCommonDataResponse = <Data extends Type<any>>(data: Data) => {
  return applyDecorators(
    ApiExtraModels(CommonDataDto, data),
    ApiOkResponse({
      schema: {
        allOf: [
          { $ref: getSchemaPath(CommonDataDto) },
          {
            properties: {
              data: { $ref: getSchemaPath(data) },
            },
          },
        ],
      },
    }),
  );
};

export const ApiCommonDataNumberResponse = () => {
  return applyDecorators(
    ApiExtraModels(CommonDataDto, CommonDataNumberDto),
    ApiOkResponse({
      schema: {
        allOf: [
          { $ref: getSchemaPath(CommonDataDto) },
          { $ref: getSchemaPath(CommonDataNumberDto) },
        ],
      },
    }),
  );
};

export const ApiCommonDataBooleanResponse = () => {
  return applyDecorators(
    ApiExtraModels(CommonDataDto, CommonDataBooleanDto),
    ApiOkResponse({
      schema: {
        allOf: [
          { $ref: getSchemaPath(CommonDataDto) },
          { $ref: getSchemaPath(CommonDataBooleanDto) },
        ],
      },
    }),
  );
};

export const ApiCommonDataStringResponse = () => {
  return applyDecorators(
    ApiExtraModels(CommonDataDto, CommonDataStringDto),
    ApiOkResponse({
      schema: {
        allOf: [
          { $ref: getSchemaPath(CommonDataDto) },
          { $ref: getSchemaPath(CommonDataStringDto) },
        ],
      },
    }),
  );
};
